package com.agileai.crm.bizmoduler.customer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.bizmoduler.core.TreeAndContentManageImpl;
import com.agileai.util.StringUtil;

public class CustomerGroup8ContentManageImpl
        extends TreeAndContentManageImpl
        implements CustomerGroup8ContentManage {
	
	protected Map<String,String> subTableIdNameMapping = new HashMap<String,String>();
	protected Map<String,String> subTableIdSortFieldMapping = new HashMap<String,String>();
	
    public CustomerGroup8ContentManageImpl() {
        super();
        this.columnIdField = "GRP_ID";
        this.columnParentIdField = "GRP_SUP_ID";
        this.columnSortField = "GRP_SORT";
    }
    
    public String[] getTableIds() {
        List<String> temp = new ArrayList<String>();

        temp.add("_base");
        temp.add("ContactPerson");
        temp.add("CustomerInfoSales");

        return temp.toArray(new String[] {  });
    }
    public List<DataRow> findSubRecords(String subId, DataParam param) {
		List<DataRow> result = null;
		String statementId = sqlNameSpace+"."+"find"+StringUtil.upperFirst(subId)+"Records";
		result = this.daoHelper.queryRecords(statementId, param);
		return result;
	}
    
    public DataRow getCustomerInfoRecord(DataParam param) {
		String statementId = sqlNameSpace+"."+"getCustomerInfoRecord";
		DataRow result = this.daoHelper.getRecord(statementId, param);
		return result;
	}

	@Override
	  public void saveSubRecords(DataParam param, List<DataParam> insertRecords, List<DataParam> updateRecords) {
			String subId = param.get("currentSubTableId");
			String curTableName = "crm_contact_person";
			if (insertRecords != null && insertRecords.size() > 0){
				String sortField = subTableIdSortFieldMapping.get(subId);
				int sortValue = 0;
				if (!StringUtil.isNullOrEmpty(sortField)){
					sortValue = getNewMaxSort(subId,param);	
				}
				for (int i=0;i < insertRecords.size();i++){
					DataParam paramRow = insertRecords.get(i);
					paramRow.put("CUST_ID", param.get("CUST_ID"));
					processDataType(paramRow, curTableName);	
					processPrimaryKeys(curTableName,paramRow);
					
					if (!StringUtil.isNullOrEmpty(sortField)){
						paramRow.put(sortField,sortValue+i);
					}
				}
			}
			String statementId = sqlNameSpace+"."+"insert"+StringUtil.upperFirst(subId)+"Record";
			this.daoHelper.batchInsert(statementId, insertRecords);
			
			statementId = sqlNameSpace+"."+"update"+StringUtil.upperFirst(subId)+"Record";
			this.daoHelper.batchUpdate(statementId, updateRecords);
		}
	
	protected int getNewMaxSort(String subId,DataParam param){
		int result = 0;
		String sortField = subTableIdSortFieldMapping.get(subId);
		String statementId = sqlNameSpace+"."+"find"+StringUtil.upperFirst(subId)+"Records";
		List<DataRow> records = this.daoHelper.queryRecords(statementId, param);
		if (records != null && records.size() > 0){
			DataRow row = records.get(records.size()-1);
			String maxMenuSort = row.stringValue(sortField);
			result = Integer.parseInt(maxMenuSort)+1;
		}else{
			result = 1;
		}
		return result;
	}
	
	public void deleteSubRecord(String subId, DataParam param) {
		String statementId = sqlNameSpace+"."+"deleteContactPersonRecord";
		this.daoHelper.deleteRecords(statementId, param);
	}

	public void updateSubmitStateInfoRecord(DataParam param) {
		String statementId = sqlNameSpace+"."+"updateSubmitStateInfoRecord";
		processDataType(param, tableName);
		this.daoHelper.updateRecord(statementId, param);
		
	}

	public void updateConfirmStateInfoRecord(DataParam param) {
		String statementId = sqlNameSpace+"."+"updateConfirmStateInfoRecord";
		processDataType(param, tableName);
		this.daoHelper.updateRecord(statementId, param);
		
	}
	
	@Override
	public void updatetConfirmRecord(String tabId, DataParam param) {
			String statementId = sqlNameSpace+"."+"updatetConfirmRecord";
			String curTableName = tabIdAndTableNameMapping.get(tabId);
			processDataType(param, curTableName);
			this.daoHelper.updateRecord(statementId, param);
	}

	@Override
	public void addUserTreeRelation(String custId, String[] groupIds) {
		String statementId = sqlNameSpace+".addUserTreeRelation";
		List<DataParam> paramList = new ArrayList<DataParam>();
		for (int i=0;i < groupIds.length;i++){
			DataParam dataParam = new DataParam("custId",custId,"userId",groupIds[i]);
			paramList.add(dataParam);
		}
		this.daoHelper.batchInsert(statementId, paramList);
	}

	@Override
	public void delUserTreeRelation(String custId, String groupId) {
		String statementId = sqlNameSpace+".delUserTreeRelation";
		DataParam param = new DataParam("custId",custId,"userId",groupId);
		this.daoHelper.deleteRecords(statementId, param);
	}

	@Override
	public List<DataRow> findRecords(DataParam param) {
		String statementId = sqlNameSpace+"."+"queryRecords";
		List<DataRow> result = this.daoHelper.queryRecords(statementId, param);
		return result;		
	}

	@Override
	public List<DataRow> findCityRecords(String proviceCode) {
		DataParam param = new DataParam("proviceCode",proviceCode);
		String statementId = sqlNameSpace+"."+"findCityRecords";
		List<DataRow> result = this.daoHelper.queryRecords(statementId, param);
		return result;	
	}

	@Override
	public DataRow getStatisticsRecords(DataParam param) {
		DataRow result = new DataRow();
		
		String statementId = sqlNameSpace + ".getClueNumRecord";
		DataRow clueNum = this.daoHelper.getRecord(statementId, param);
		
		statementId = sqlNameSpace + ".getVisitNumRecord";
		DataRow visitNum = this.daoHelper.getRecord(statementId, param);
		
		statementId = sqlNameSpace + ".getOppNumRecord";
		DataRow oppNum = this.daoHelper.getRecord(statementId, param);
		
		statementId = sqlNameSpace + ".getOrderNumRecord";
		DataRow orderNum = this.daoHelper.getRecord(statementId, param);
		
		
		result.put("STATIS_CLUE",clueNum.get("STATIS_CLUE"));
		result.put("STATIS_VISIT",visitNum.get("STATIS_VISIT"));
		result.put("STATIS_OPP",oppNum.get("STATIS_OPP"));
		result.put("STATIS_ORDER",orderNum.get("STATIS_ORDER"));
		
		return result;
	}

	@Override
	public List<DataRow> findPersonRecords(DataParam param) {
		String statementId = sqlNameSpace+"."+"findContactPersonRecords";
		List<DataRow> result = this.daoHelper.queryRecords(statementId, param);
		return result;	
	}

	@Override
	public List<DataRow> findSalesRecords(DataParam param) {
		String statementId = sqlNameSpace+"."+"findDetail";
		List<DataRow> result = this.daoHelper.queryRecords(statementId, param);
		return result;
	} 
  
}
